﻿
using LeoK.TM;
using System;
using System.ComponentModel;
using System.Globalization;
using System.Reflection;
using System.Threading;
using System.Windows;
using System.Windows.Forms;
using System.Windows.Media.Imaging;
using LeoK.Common;
using LeoK.DCI.Views;
using LeoK.RP;
using Telerik.Windows.Controls;

namespace LeoK
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        private NotifyIcon _notifyIcon;
        private QuickTaskWindow _quickTaskWindow = new QuickTaskWindow();

        public MainWindow()
        {
            StyleManager.ApplicationTheme = new Windows8Theme();
            Thread.CurrentThread.CurrentCulture = new CultureInfo("zh-CN");
            Thread.CurrentThread.CurrentUICulture = new CultureInfo("zh-CN");
            RadWindow.Prompt(new DialogParameters()
            {
                Header = "验证",
                Content = "密码",
                OkButtonContent = "确定",
                CancelButtonContent = "取消",
                Closed = (sender1, e1) =>
                {
                    if (e1.DialogResult.HasValue && e1.DialogResult.Value)
                    {
                        string pwd = e1.PromptResult;

                        try
                        {
                            MySqlDB.SetPWD(pwd);
                            LocalizationManager.Manager = new LocalizationManager()
                            {
                                ResourceManager = LeoK.Properties.Resources.ResourceManager
                            };

                            InitializeComponent();

                            ShowInTaskbar = false;

                            InitNotifyIcon();
                            Hide();
                            WindowState = WindowState.Minimized;
                        }
                        catch (Exception)
                        {
                            RadWindow.Alert("密码错误");
                            App.Current.Shutdown();
                        }
                    }
                    else
                    {
                        App.Current.Shutdown();
                    }
                }
            });
        }

        protected override void OnClosing(CancelEventArgs e)
        {
            e.Cancel = true;
            ShowInTaskbar = false;
            Hide();
        }

        private void InitNotifyIcon()
        {
            _notifyIcon = new NotifyIcon();
#if DEBUG
            _notifyIcon.Text = "LeoK WorkStation(Debug)" + Assembly.GetExecutingAssembly().GetName().Version;
#else
            _notifyIcon.Text = "LeoK WorkStation(Release)" + Assembly.GetExecutingAssembly().GetName().Version; ;
#endif
            _notifyIcon.Icon = System.Drawing.Icon.ExtractAssociatedIcon(System.Windows.Forms.Application.ExecutablePath);
            _notifyIcon.Visible = true;

            System.Windows.Forms.ContextMenu menu = new System.Windows.Forms.ContextMenu();
            
            System.Windows.Forms.MenuItem taskMenuItem = new System.Windows.Forms.MenuItem();
            taskMenuItem.Text = "任务管理";
            taskMenuItem.Click += TaskMenuItemOnClick;
            menu.MenuItems.Add(taskMenuItem);

            System.Windows.Forms.MenuItem dciMenuItem = new System.Windows.Forms.MenuItem();
            dciMenuItem.Text = "DCI数据管理";
            dciMenuItem.Click += DciMenuItem_Click;
            menu.MenuItems.Add(dciMenuItem);

            System.Windows.Forms.MenuItem reportMenuItem = new System.Windows.Forms.MenuItem();
            reportMenuItem.Text = "分析报告";
            reportMenuItem.Click += ReportMenuItem_Click;
            menu.MenuItems.Add(reportMenuItem);

            System.Windows.Forms.MenuItem exitMenuItem = new System.Windows.Forms.MenuItem();
            exitMenuItem.Text = "退出系统";
            exitMenuItem.Click += ExitMenuItem_Click; ;
            menu.MenuItems.Add(exitMenuItem);

            _notifyIcon.ContextMenu = menu;
            _notifyIcon.Click += NotifyIcon_Click;
        }


        private void NotifyIcon_Click(object sender, EventArgs e)
        {
            if ((e as System.Windows.Forms.MouseEventArgs).Button == MouseButtons.Left)
            {
                if (_quickTaskWindow.Visibility == Visibility.Visible)
                {
                    _quickTaskWindow.Hide();
                }
                else
                {
                    _quickTaskWindow.LoadData();
                    _quickTaskWindow.Show();
                }
            }
        }

        private void ExitMenuItem_Click(object sender, EventArgs e)
        {
            RadWindow.Confirm("确定要退出吗？", (sender1, e1) =>
            {
                if (e1.DialogResult.Value)
                {
                    App.Current.Shutdown();
                }
            });
        }

        private void DciMenuItem_Click(object sender, EventArgs e)
        {
            Icon = new BitmapImage(new Uri("pack://application:,,,/Resources/ucDCI.png", UriKind.RelativeOrAbsolute));
            Title = "DCI 数据管理";
            mainWindow.Content = new DCIChart();
            WindowState = WindowState.Maximized;
            Show();
            ShowInTaskbar = true;
        }

        private void TaskMenuItemOnClick(object sender, EventArgs eventArgs)
        {
            Icon = new BitmapImage(new Uri("pack://application:,,,/Resources/ucPM.png", UriKind.RelativeOrAbsolute));
            Title = "任务管理";
            mainWindow.Content = new TaskFrame();
            WindowState = WindowState.Maximized;
            Show();
            ShowInTaskbar = true;
        }

        private void ReportMenuItem_Click(object sender, EventArgs e)
        {
            Icon = new BitmapImage(new Uri("pack://application:,,,/Resources/ucFin.png", UriKind.RelativeOrAbsolute));
            Title = "分析报告";
            mainWindow.Content = new ReportFrame();
            WindowState = WindowState.Maximized;
            Show();
            ShowInTaskbar = true;
        }

    }
}
